***********************************************************************************
***	Replication file for:                                                     	***
*** Braun, S. T. and Stuhler, J. (2024). The Economic Consequences of 			***
***	Being Widowed by War: A Life-Cycle Perspective.	     						***
***																				***
***	Journal of Public Economics                                                 ***
***   							                                                ***
*** Script:		_3-ghs-1-prep-death-parents.do					   			 	***	
*** Purpose:	data preparation, death or absence oarents, GHS-1				***
***																				***
***********************************************************************************


**************************************************
* Death / absence father
**************************************************

**************************************************
* RECORD 1 PA – Allgemeine Angaben
* RECORD 1 PA – General information
**************************************************

use "$widowsghs/data/LV1/REC1.DTA", clear

tab A141
recode A141 96/99 = .
rename A141 geburtsjahr
label var geburtsjahr "Year of birth of index person"

gen kohorte = 2 if geburtsjahr >= 29 & geburtsjahr <= 31 
replace kohorte = 3 if geburtsjahr >= 39 & geburtsjahr <= 41
replace kohorte = 4 if geburtsjahr >= 49 & geburtsjahr <= 51

tab A169 
recode A169 996/999 988 = .
rename A169 geburtsjahr_vater
label var geburtsjahr_vater "Year of birth of the father"

tab A170
recode A170 6/9 = .
recode A170 0 = .
rename A170 vater_lebendig
label var vater_lebendig "Is father still alive?"

tab A171
recode A171 96/99 = .
recode A171 0 = .
rename A171 todesjahr_vater
label var todesjahr_vater "Year of death of the father"

keep A01 geburt* vater* todes* geburtsjahr* kohorte

compress

save "$widowsghs/processed/intermediate/LV1_todesjahr_vater.dta", replace


**************************************************
* RECORD 2 TRENNVA – Trennung vom Vater 
* RECORD 2 TRENNVA – Separation from the father
**************************************************

*** Note: Data record separation from father only
*** within the first 16 years of life

use "$widowsghs/data/LV1/TRENNVA.DTA", clear

*** Relevant labels B04 / B05 (reasons for absence)

* 20 = Krieg
* 21 = Militärdienst
* 22 = Gefangenschaft
* 23 = Flucht bzw. Ausweisung
* 24 = vermisst
* 25 = gefallen
* 34 = KZ
* 
* zusätzlich eigene Person:
* 56 = Kinderlandverschickung
* 60 = Evakuierung der Kinder
* 62 = Flucht des Befragten

gen abwesenheit_krieg = 0
replace abwesenheit_krieg = 1 if (B04 == 20 | B04 == 21 | B04 == 22 | B04 == 23 | B04 == 24 | B04 == 25 | B04 == 34) | (B05 == 20 | B05 == 21 | B05 == 22 | B05 == 23 | B05 == 24 | B05 == 25 | B05 == 34)
label var abwesenheit_krieg "Father absent because of war?" 

gen abwesenheit_krieg_v2 = 0
replace abwesenheit_krieg_v2 = 1 if (B04 == 20 | B04 == 21 | B04 == 22 | B04 == 23 | B04 == 24 | B04 == 25 | B04 == 34 | B04 == 56 | B04 == 60 | B04 == 62) | (B05 == 20 | B05 == 21 | B05 == 22 | B05 == 23 | B05 == 24 | B05 == 25 | B05 == 34 | B04 == 56 | B04 == 60 | B04 == 62)
label var abwesenheit_krieg_v2 "Father absent due to war (also due to war-related absence of the child)?" 

merge m:m A01 using "$widowsghs/processed/intermediate/LV1_todesjahr_vater.dta"

* Fill father's year of death for a few observations from information in TRENNVA record

* Year of death unknown, but year of death mentioned in the TRENNVA Record
recode todesjahr_vater . = 40 if A01 == 10396
recode todesjahr_vater . = 43 if A01 == 12164

* Year of death unknown, missing in action => select first year of war service
recode todesjahr_vater . = 42 if A01 == 11490
recode todesjahr_vater . = 41 if A01 == 11903
recode todesjahr_vater . = 40 if A01 == 12208
recode todesjahr_vater . = 40 if A01 == 11024
recode vater_lebendig . = 2 if A01 == 11024

* Correct end of spell after death of father (end=year in which person reaches 16 years of age)
recode B03 0 = 45 if A01 == 11624

**************************************************
* Variables for death/absence of father
************************************************** 

*** Father died in the war?

* Only defined for cohort 2 (1929-31) and 3 (1939-41) (as cohort 4 born after the war)

* Criteria:
* Case 1: Did not die in the war
* 1. still alive
* 2. does not meet any of the criteria below and died after 1945 or before 1939
*
* Case 2: Died in the war
* 1. Fallen in the war
* 2. Death after absence due to war
* 3. Missing in action
* 4. Absence due to war, year of death in the spell of absence due to war

* Version 1: Those who died between 1939 and 45 and do not fulfill any of the criteria 1-4 are coded as missing 
* Version 2: Those who died between 1939 and 45 are counted as war dead

gen tod_vater_krieg_v1 = . 

replace tod_vater_krieg_v1 = 0 if vater_lebendig == 1 & (kohorte == 2 | kohorte == 3)
replace tod_vater_krieg_v1 = 1 if vater_lebendig == 2 & (B04 == 25 | B05 == 25)
replace tod_vater_krieg_v1 = 1 if vater_lebendig == 2 & (abwesenheit_krieg == 1 & B05 == 40)
replace tod_vater_krieg_v1 = 1 if vater_lebendig == 2 & (todesjahr_vater >= 39 & todesjahr_vater <= 45) & (B04 == 24 | B05 == 24)
replace tod_vater_krieg_v1 = 1 if vater_lebendig == 2 & (todesjahr_vater >= 39 & todesjahr_vater <= 45) & (todesjahr_vater >= B02 & todesjahr_vater <= B03) & abwesenheit_krieg == 1

recode tod_vater_krieg_v1 . = 0 if vater_lebendig == 2 & todesjahr_vater > 45 & (kohorte == 2 | kohorte == 3)
recode tod_vater_krieg_v1 . = 0 if vater_lebendig == 2 & todesjahr_vater < 39 & (kohorte == 2 | kohorte == 3)

gen 	tod_vater_krieg_v2 = tod_vater_krieg_v1
recode  tod_vater_krieg_v2 . = 1 if (todesjahr_vater >= 39 & todesjahr_vater <= 45) & (kohorte == 2 | kohorte == 3)

* Death during the years 1939 and 1945

gen 	tod_vater_1939_45 = .
replace tod_vater_1939_45 = 0 if vater_lebendig == 1 
replace tod_vater_1939_45 = 0 if (todesjahr_vater < 39 | todesjahr_vater > 45) & todesjahr_vater != .
replace tod_vater_1939_45 = 1 if (todesjahr_vater >= 39 & todesjahr_vater <= 45)
label var tod_vater_1939_45 "Did father die between 1939 and 1945?"


*** Duration of the father's absence

recode B02 0 = .

* Dummies for years of life (0-16)
forvalues t = 0/16{
gen help_`t' = 0
replace help_`t' = 1 if (B02-geburtsjahr) <= `t' & (B03-geburtsjahr) >= `t'
bysort A01: egen d_vater_abw_`t' = max(help_`t')
label var d_vater_abw_`t' "Was father absent at age `t'?"
drop help_`t'
}

* Count years
gen vater_jahre_abw = 0
forvalues t = 0/16{
replace vater_jahre_abw = vater_jahre_abw + d_vater_abw_`t'
}
label var vater_jahre_abw "Number of years in which the father was absent until the age of 16"


*** Duration of the father's absence due to war

* Dummies for age/years of life (0-16)
forvalues t = 0/16{
gen help_`t' = 0
replace help_`t' = 1 if (B02-geburtsjahr) <= `t' & (B03-geburtsjahr) >= `t' & (abwesenheit_krieg_v2 == 1 | tod_vater_krieg_v2 == 1) 
bysort A01: egen d_vater_abw_krieg_`t' = max(help_`t')
label var d_vater_abw_krieg_`t' "Was father absent due to war at age `t'?"
drop help_`t'
}

* Count
gen vater_jahre_abw_krieg = 0
forvalues t = 0/16{
replace vater_jahre_abw_krieg = vater_jahre_abw_krieg + d_vater_abw_krieg_`t'
}
label var vater_jahre_abw_krieg "Number of years in which the father was absent due to war until the age of 16"

* Correction: Persons with incomplete information on duration of absence 
* Father was absent, duration unknown => missing
gen help = 1 if (B02 == 0 | B03 == 0 | B02 == . | B03 == .) & B01 != .
bysort A01: egen help2 = max(help)
drop help

replace vater_jahre_abw = . if help2 == 1
replace vater_jahre_abw_krieg = . if help2 == 1
drop help2

*** Generate variables indicating for each person whether father died in the war 
*** (based on variables for different episodes of absence of which there might be more than one per person)

bysort A01: egen help = max(tod_vater_krieg_v1)
drop tod_vater_krieg_v1
rename help tod_vater_krieg_v1
label var tod_vater_krieg_v1 "Did the father die in war operations?"

bysort A01: egen help = max(tod_vater_krieg_v2)
drop tod_vater_krieg_v2
rename help tod_vater_krieg_v2
label var tod_vater_krieg_v2 "Did the father die in war operations or during the war?"

*** Record years of births / death to consistent metric
replace geburtsjahr = geburtsjahr + 1900
replace geburtsjahr_vater = geburtsjahr_vater + 1000
replace todesjahr_vater = todesjahr_vater + 1900

*** Keep one observation per individual and save

gen lebensverlaufsstudie = 1
label var lebensverlaufsstudie "GHS wave"

sort A01
drop if A01 == A01[_n-1] 

rename A01 AIDN

keep AIDN geburts* vater_lebendig todesjahr_vater vater_jahre_abw vater_jahre_abw_krieg tod_vater_krieg_v1 tod_vater_krieg_v2 d_vater_abw* tod_vater_1939_45
aorder

save "$widowsghs/processed/intermediate/Tod_vater_abwesenheit_Vater_LV1.dta", replace

**************************************************
* Death / absence mother
**************************************************

**************************************************
* RECORD 1 PA – Allgemeine Angaben
* RECORD 1 PA – General information
**************************************************

use "$widowsghs/data/LV1/REC1.DTA", clear

tab A141
recode A141 96/99 = .
rename A141 geburtsjahr
replace geburtsjahr = geburtsjahr 
label var geburtsjahr "Year of birth of index person"

gen kohorte = 2 if geburtsjahr >= 29 & geburtsjahr <= 31 
replace kohorte = 3 if geburtsjahr >= 39 & geburtsjahr <= 41
replace kohorte = 4 if geburtsjahr >= 49 & geburtsjahr <= 51

tab A142
recode A142 989 996/999 = .
rename A142 geburtsjahr_mutter
label var geburtsjahr_mutter "Year of birth of the mother"

tab A143
recode A143 6/9 = .
recode A143 0 = .
rename A143 mutter_lebendig
label var mutter_lebendig "Is mother still alive?"

tab A144
recode A144 96/99 = .
recode A144 0 = .
rename A144 todesjahr_mutter
label var todesjahr_mutter "Year of death of the mother"


keep A01 geburt* mutter* todes* geburtsjahr* kohorte

compress

save "$widowsghs/processed/intermediate/LV1_todesjahr_mutter.dta", replace

**************************************************
* RECORD 3 TRENNMA – Trennung von der Mutter
* RECORD 3 TRENNMA – Separation from the mother
**************************************************

use "$widowsghs/data/LV1/TRENNMA.DTA", clear

*** Relevant labels C04 / C05 (reasons for absence)

* 20 = Krieg
* 21 = Militärdienst
* 22 = Gefangenschaft
* 23 = Flucht bzw. Ausweisung
* 24 = vermisst
* 25 = gefallen
* 34 = KZ
* 
* zusätzlich eigene Person:
* 56 = Kinderlandverschickung
* 60 = Evakuierung der Kinder
* 62 = Flucht des Befragten


gen abwesenheit_krieg = 0
replace abwesenheit_krieg = 1 if (C04 == 20 | C04 == 21 | C04 == 22 | C04 == 23 | C04 == 24 | C04 == 25 | C04 == 34) | (C05 == 20 | C05 == 21 | C05 == 22 | C05 == 23 | C05 == 24 | C05 == 25 | C05 == 34)
label var abwesenheit_krieg "Mother absent because of war?" 

gen abwesenheit_krieg_v2 = 0
replace abwesenheit_krieg_v2 = 1 if (C04 == 20 | C04 == 21 | C04 == 22 | C04 == 23 | C04 == 24 | C04 == 25 | C04 == 34 | C04 == 56 | C04 == 60 | C04 == 62) | (C05 == 20 | C05 == 21 | C05 == 22 | C05 == 23 | C05 == 24 | C05 == 25 | C05 == 34 | C04 == 56 | C04 == 60 | C04 == 62)
label var abwesenheit_krieg_v2 "Mother absent due to war (also due to war-related absence of the child)?" 

merge m:m A01 using "$widowsghs/processed/intermediate/LV1_todesjahr_mutter.dta"


**************************************************
* Variables for absence of the mother
************************************************** 

* Dummies for age/years of life (0-16)
forvalues t = 0/16{
gen help_`t' = 0
replace help_`t' = 1 if (C02-geburtsjahr) <= `t' & (C03-geburtsjahr) >= `t'
bysort A01: egen d_mutter_abw_`t' = max(help_`t')
label var d_mutter_abw_`t' "Was mother absent at age `t'?"
drop help_`t'
}


gen mutter_jahre_abw = 0
forvalues t = 0/16{
replace mutter_jahre_abw = mutter_jahre_abw + d_mutter_abw_`t'
}
label var mutter_jahre_abw "Number of years in which the mother was absent until the age of 16"


*** Record years of births / death to consistent metric
replace geburtsjahr = geburtsjahr + 1900
replace geburtsjahr_mutter = geburtsjahr_mutter + 1000
replace todesjahr_mutter = todesjahr_mutter + 1900

*** Keep one observation per person and save

gen lebensverlaufsstudie = 1
label var lebensverlaufsstudie "GHS wave"

sort A01
drop if A01 == A01[_n-1] 

rename A01 AIDN

keep AIDN geburtsjahr* mutter_jahre_abw todesjahr_mutter mutter_lebendig d_mutter_abw*

save "$widowsghs/processed/intermediate/Tod_Abwesenheit_Mutter_LV1.dta", replace

*************************************************************
* Combine and order data
**************************************************************

use "$widowsghs/processed/intermediate/Tod_vater_abwesenheit_Vater_LV1.dta", clear
merge m:m AIDN using "$widowsghs/processed/intermediate/Tod_Abwesenheit_Mutter_LV1.dta"
drop _merge

gen ___IDs______________ = .
rename AIDN fid
order ___IDs______________ fid, first

gen ___Demographics_____ = .
order ___Demographics_____ geburtsjahr, after(fid)

gen ___Death_or_absence_father___ = .
order ___Death_or_absence_father___ vater_jahre_abw vater_jahre_abw_krieg d_vater_abw_0- d_vater_abw_krieg_16 geburtsjahr_vater todesjahr_vater tod_vater_1939_45 tod_vater_krieg_v1 tod_vater_krieg_v2 vater_lebendig, after(geburtsjahr)

gen ___Death_or_absence_mother___ = .
order ___Death_or_absence_mother___ d_mutter_abw_0- d_mutter_abw_16 mutter_jahre_abw geburtsjahr_mutter todesjahr_mutter mutter_lebendig, after(vater_lebendig)

compress
save "$widowsghs/processed/intermediate/LV1_shock.dta", replace

